﻿function AutomateWPS(tableid,count,num,title)
{
    try
    {
        //        var curTbl = document.getElementById(tableid);
        var oXL = new ActiveXObject("ET.Application"); 
        if (oXL == null)
        {
            alert('导出WPS失败，确定是否安装了WPS，浏览器设置是否正确');
        }else{
            //创建AX对象excel 
            var oWB = oXL.Workbooks.Add(); 
            //获取workbook对象 
            var oSheet = oWB.ActiveSheet; 
            //激活当前sheet 
            var Lenr =tableid.rows.length;// curTbl.rows.length;
            //取得表格行数 

            var letter = getLetter(count);
            var merge='';
            merge ='A1:'+letter+'1';
            oXL.Range(merge).MergeCells = true;  //合并单元格
            merge ='A2:'+letter+'2';
            oXL.Range(merge).MergeCells = true;  //合并单元格  
                        
            if(title=='综合指标统计'){
                merge ='A3:A5';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='H3:H5';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='I3:I5';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                
                merge ='A6:A8';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='H6:H8';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='I6:I8';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='A9:A10';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='H9:H10';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='I9:I10';
                oXL.Range(merge).MergeCells = true;  //合并单元格
                
                oSheet.Cells(1,1).HorizontalAlignment = 3;
                oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
                oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
                
                oSheet.Columns("A:G").ColumnWidth = 15;  //设置单元格宽度
                 
                for(j=3;j<num+3;j++){
                    oSheet.Cells(j,3).HorizontalAlignment = 4;
                    oSheet.Cells(j,5).HorizontalAlignment = 4;
                    oSheet.Cells(j,7).HorizontalAlignment = 4;
                    oSheet.Cells(j,9).HorizontalAlignment = 4;
                } 
                               
                for (i = 0; i < Lenr; i++) 
                { 
                    var Lenc = tableid.rows(i).cells.length; 
                    //取得每行的列数 
                    for (j = 0; j < Lenc; j++) 
                    {
                        var cellValue;
                        cellValue = tableid.rows(i).cells(j).innerText;
                        if(j==1 || j==2)
                        {
                            cellValue = "'"+cellValue; //转换为文本格式
                        }  
                        if(i==3||i==4||i==6||i==7||i==9){
                            oSheet.Cells(i + 1, j + 2).value = cellValue;    
                        }
                        else{
                            oSheet.Cells(i + 1, j + 1).value = cellValue;   
                        }
                    } 
                }  
                
            }else if(title=='电能质量技术监督报表')
            {
                merge ='A3:A4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                
                merge ='B3:C3';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='D3:E3';
                oXL.Range(merge).MergeCells = true;  //合并单元格

                oSheet.Cells(1,1).HorizontalAlignment = 3;
                oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
                oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
                
                oSheet.Columns("A:F").ColumnWidth = 15;  //设置单元格宽度
                
                oSheet.Cells(3,1).HorizontalAlignment = 3;
                oSheet.Cells(3,2).HorizontalAlignment = 3;
                oSheet.Cells(3,3).HorizontalAlignment = 3;
                oSheet.Cells(3,4).HorizontalAlignment = 3;
                oSheet.Cells(3,5).HorizontalAlignment = 3;
                oSheet.Cells(3,6).HorizontalAlignment = 3;
                oSheet.Cells(4,5).HorizontalAlignment = 3;
                oSheet.Cells(4,6).HorizontalAlignment = 3;
                oSheet.Cells(4,2).HorizontalAlignment = 3;
                oSheet.Cells(4,3).HorizontalAlignment = 3;
                oSheet.Cells(4,4).HorizontalAlignment = 3;
                
                for(k=5;k<num+6;k++)                
                {
                    oSheet.Cells(k,6).HorizontalAlignment = 4;
                    oSheet.Cells(k,2).HorizontalAlignment = 4;
                    oSheet.Cells(k,3).HorizontalAlignment = 4;
                    oSheet.Cells(k,4).HorizontalAlignment = 4;
                    oSheet.Cells(k,5).HorizontalAlignment = 4;
                }
                
                for (i = 0; i < Lenr; i++) 
                { 
                    var Lenc = tableid.rows(i).cells.length; 
                    //取得每行的列数 
                    for (j = 0; j < Lenc; j++) 
                    {
                        var cellValue;
                        cellValue = tableid.rows(i).cells(j).innerText;
                        if(j==1 || j==2)
                        {
                            cellValue = "'"+cellValue; //转换为文本格式
                        } 
                        if(i==2){
                            if(j==2)
                                oSheet.Cells(i + 1, j + 2).value = cellValue; 
                            else if(j==3)
                                oSheet.Cells(i + 1, j + 3).value = cellValue; 
                            else
                               oSheet.Cells(i + 1, j + 1).value = cellValue; 
                        }
                        else if(i==3)
                        {
                            oSheet.Cells(i + 1, j + 2).value = cellValue;     
                        }else{
                            oSheet.Cells(i + 1, j + 1).value = cellValue;   
                        }
                    } 
                }
            }else if(title=='机组事件')
            {
                merge ='A3:A4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='C3:D3';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='B3:B4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='E3:E4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='F3:F4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 

                oSheet.Cells(1,1).HorizontalAlignment = 3;
                oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
                oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
                
                oSheet.Columns("A:F").ColumnWidth = 20;  //设置单元格宽度
                
                oSheet.Cells(3,1).HorizontalAlignment = 3;
                oSheet.Cells(3,2).HorizontalAlignment = 3;
                oSheet.Cells(3,3).HorizontalAlignment = 3;
                oSheet.Cells(3,4).HorizontalAlignment = 3;
                oSheet.Cells(3,5).HorizontalAlignment = 3;
                oSheet.Cells(3,6).HorizontalAlignment = 3;
                oSheet.Cells(4,3).HorizontalAlignment = 3;
                oSheet.Cells(4,4).HorizontalAlignment = 3;
                
                for(k=5;k<num+6;k++)                
                {
                    oSheet.Cells(k,1).HorizontalAlignment = 4;
                    oSheet.Cells(k,2).HorizontalAlignment = 4;
                    oSheet.Cells(k,3).HorizontalAlignment = 4;
                    oSheet.Cells(k,4).HorizontalAlignment = 4;
                    oSheet.Cells(k,5).HorizontalAlignment = 4;
                }
                
                oSheet.Cells(5,3).HorizontalAlignment = 4; 
                oSheet.Cells(7,3).HorizontalAlignment = 4; 
                

                for (i = 0; i < Lenr; i++) 
                { 
                    var Lenc = tableid.rows(i).cells.length; 
                    //取得每行的列数 
                    for (j = 0; j < Lenc; j++) 
                    {
                        var cellValue;
                        cellValue = tableid.rows(i).cells(j).innerText;
                        if(j==1 || j==2)
                        {
                            cellValue = "'"+cellValue; //转换为文本格式
                        } 
                        if(i==2){
                            if(j>2)
                                oSheet.Cells(i + 1, j + 2).value = cellValue;   
                            else
                                oSheet.Cells(i + 1, j + 1).value = cellValue;   
                        }
                        else if(i==3)
                        {
                            oSheet.Cells(i + 1, j + 3).value = cellValue;     
                        }else{
                            oSheet.Cells(i + 1, j + 1).value = cellValue;   
                        }
                    } 
                }
            }else if(title=='电煤油日报表')
            {
                merge ='A3:A5';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='J3:J4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='K3:K4';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                         
                merge ='A6:A8';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='J6:J7';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='K6:K7';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                
                merge ='A9:K9';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='A10:A17';
                oXL.Range(merge).MergeCells = true;  //合并单元格                 
                merge ='H11:H17';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='I11:I17';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='J11:J17';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='K11:K17';
                oXL.Range(merge).MergeCells = true;  //合并单元格  
                            
                merge ='A18:K18';
                oXL.Range(merge).MergeCells = true;  //合并单元格   
                merge ='A19:A21';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='I19:I20';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='J19:J20';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='K19:K20';
                oXL.Range(merge).MergeCells = true;  //合并单元格             
                               
                merge ='A22:A24';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='I22:I23';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='J22:J23';
                oXL.Range(merge).MergeCells = true;  //合并单元格 
                merge ='K22:K23';
                oXL.Range(merge).MergeCells = true;  //合并单元格 

                oSheet.Cells(1,1).HorizontalAlignment = 3;
                oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
                oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
                oSheet.Cells(18,1).HorizontalAlignment = 3;
                oSheet.Columns("A:A").ColumnWidth = 10;  //设置单元格宽度
                oSheet.Columns("C:K").ColumnWidth = 18;  //设置单元格宽度
                
                for(k=3;k<13;k++){                
                    oSheet.Cells(3,k).HorizontalAlignment = 3; 
                    oSheet.Cells(6,k).HorizontalAlignment = 3;
                    oSheet.Cells(10,k).HorizontalAlignment = 3;
                    oSheet.Cells(19,k).HorizontalAlignment = 3;
                    oSheet.Cells(22,k).HorizontalAlignment = 3;                
                }  
                
                oSheet.Cells(4,3).HorizontalAlignment = 4;
                oSheet.Cells(5,3).HorizontalAlignment = 4; 
                oSheet.Cells(7,3).HorizontalAlignment = 4; 
                oSheet.Cells(8,3).HorizontalAlignment = 4;
                oSheet.Cells(4,4).HorizontalAlignment = 4; 
                oSheet.Cells(5,4).HorizontalAlignment = 4; 
                oSheet.Cells(7,4).HorizontalAlignment = 4; 
                oSheet.Cells(8,4).HorizontalAlignment = 4;  

                for(j=11;j<18;j++){
                    oSheet.Cells(j,3).HorizontalAlignment = 4;
                    oSheet.Cells(j,4).HorizontalAlignment = 4;
                    oSheet.Cells(j,5).HorizontalAlignment = 4;
                    oSheet.Cells(j,6).HorizontalAlignment = 4;
                    oSheet.Cells(j,7).HorizontalAlignment = 4;
                }
                
                oSheet.Cells(20,3).HorizontalAlignment = 4; 
                oSheet.Cells(21,3).HorizontalAlignment = 4; 
                oSheet.Cells(23,3).HorizontalAlignment = 4; 
                oSheet.Cells(24,3).HorizontalAlignment = 4;
                oSheet.Cells(21,4).HorizontalAlignment = 4; 
                oSheet.Cells(22,4).HorizontalAlignment = 4; 
                oSheet.Cells(23,4).HorizontalAlignment = 4; 
                oSheet.Cells(24,4).HorizontalAlignment = 4;   
                         
                for (i = 0; i < Lenr; i++) 
                { 
                    var Lenc = tableid.rows(i).cells.length; 
                    //取得每行的列数 
                    for (j = 0; j < Lenc; j++) 
                    {
                        var cellValue;
                        cellValue = tableid.rows(i).cells(j).innerText;
                        if(j==1 || j==2)
                        {
                            cellValue = "'"+cellValue; //转换为文本格式
                        }  
                        if(i==3||i==4||i==6||i==7||i==10||i==11||i==11||i==12||i==13||i==14||i==15||i==16||i==19||i==20||i==22||i==23){
                            oSheet.Cells(i + 1, j + 2).value = cellValue;    
                        }
                        else{
                            oSheet.Cells(i + 1, j + 1).value = cellValue;   
                        }
                    } 
                } 
            }else
            {
                oSheet.Cells(1,1).HorizontalAlignment = 3;
                oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
                oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
                oSheet.Columns("A:A").ColumnWidth = 30;  //设置单元格宽度
                 for(j=2;j<count+3;j++){
                    oSheet.Cells(3,j).HorizontalAlignment = 3;
                }
                for(j=4;j<num+5;j++){
                    oSheet.Cells(j,3).HorizontalAlignment = 4;
                    oSheet.Cells(j,2).HorizontalAlignment = 3;
                }
                for (i = 0; i < Lenr; i++) 
                { 
                    var Lenc = tableid.rows(i).cells.length; 
                    //取得每行的列数 
                    for (j = 0; j < Lenc; j++) 
                    {
                        var cellValue;
                        cellValue = tableid.rows(i).cells(j).innerText;
                        if(j==1 || j==2)
                        {
                            cellValue = "'"+cellValue; //转换为文本格式
                        }  
                        oSheet.Cells(i + 1, j + 1).value = cellValue; 
                    } 
                } 
            }
            //设置excel可见属性
            oXL.Visible = true;
        }
    }catch(e){
        alert("导出WPS失败，确定是否安装了WPS");

        oXL = null; 
        oWB = null; 
        oSheet = null;
    }
    //oSheet.Columns.AutoFit; 
    //oXL.UserControl = true;  //wps交由用户控制 很重要
}
//function copy(tableid) 
//{ 
//var oControlRange = document.body.createControlRange(); 
//oControlRange.add(tableid,0); 
//oControlRange.select(); 
//document.execCommand("Copy"); 
//} 
//function AutomateWPS(tableid){ 
//copy(tableid); 
//try 
//{ 
//var xls = new ActiveXObject( "ET.Application" ); 
//} 
//catch(e) 
//{ 
//alert( "Excel没有安装或浏览器设置不正确.请启用所有Active控件和插件"); 
//return false; 
//} 
//xls.visible = true; 
//var xlBook = xls.Workbooks.Add; 
//var xlsheet = xlBook.Worksheets(1); 
//xlBook.Worksheets(1).Activate; 
//for(var i=0;i<tableid.rows(0).cells.length;i++){ 
//xlsheet.Columns(i+1).ColumnWidth=15; 
//} 
//xlsheet.Paste; 
//xls=null; 
//idTmr = window.setInterval("Cleanup();",1); 
//} 
//function Cleanup() { 
//window.clearInterval(idTmr); 
//CollectGarbage(); 
//} 